using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Sanchay
{
    public partial class AddEditSpareParts : Form
    {
        DataGridView dgv;
        int rowIndex;
        string partsNo,partsName;
        bool isValid, isEdit;
        decimal currentMRP;
       
        public AddEditSpareParts(DataGridView dgv1, int rowIndex1, string partsNo)
        {
            this.dgv = dgv1;
            this.rowIndex = rowIndex1;
            this.partsNo = partsNo;
            InitializeComponent();
        }

        public AddEditSpareParts(string partsNo, string partsName, decimal currentMRP)
        {
            this.partsNo = partsNo;
            this.partsName = partsName;
            this.currentMRP = currentMRP;
            isEdit = true;
            InitializeComponent();
        }

        private void AddSpareParts_Load(object sender, EventArgs e)
        {
           
            txtPartNo.Text = partsNo;
            if (isEdit)
            {
                txtPartNo.Enabled = false;
                txtPartName.Enabled = false;
                txtPartName.Text = partsName;
                txtMrp.Text = currentMRP.ToString();
            }
        }

        //
        // Save
        //
        private void btnSave_Click(object sender, EventArgs e)
        {
            // Mandatory Fields
            Control[] MandatoryControls = new Control[3];
            MandatoryControls[0] = txtPartNo;
            MandatoryControls[1] = txtPartName;
            MandatoryControls[2] = txtMrp;
         

            if (Validation.ValidateEmpty(MandatoryControls) && Validation.CheckIfNumeric(txtMrp.Text))
            {
                string query="";
                DataAccess da = new DataAccess();
                if (!isEdit) // ADD
                {

                     query = "INSERT INTO SpareParts (PartsNo, PartsName, BikeId, CurrentMRP,AvailableQty) VALUES (@PartsNo, @PartsName, @BikeId,@CurrentMRP,@AvailableQty)";
                    SqlParameter[] param = new SqlParameter[5];

                    param[0] = new SqlParameter("PartsNo", txtPartNo.Text);
                    param[1] = new SqlParameter("PartsName", txtPartName.Text);
                    param[2] = new SqlParameter("BikeId", 1);
                    param[3] = new SqlParameter("CurrentMRP", txtMrp.Text);
                    param[4] = new SqlParameter("AvailableQty", 1);

                    da.DoCommand(query, param);
                    dgv.Rows[rowIndex].Cells[0].Value = txtPartNo.Text;
                    dgv.Rows[rowIndex].Cells[1].Value = txtPartName.Text;
                   
                }
                else // Update
                {
                    query = "UPDATE SpareParts SET CurrentMRP = " + txtMrp.Text + " WHERE PartsNo = '" + partsNo + "'";
                    da.DoCommand(query);
                    
                }
                isValid = true;
                this.Close();
            }
            else
                isValid = false;

            if (!Validation.CheckIfNumeric(txtMrp.Text))
                txtMrp.BackColor = Color.Pink;
            else
                txtMrp.BackColor = Color.White;
        }

        private void AddSpareParts_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!isEdit)
            {
                if (!isValid)
                    e.Cancel = true;
            }
        }

        //
        // Cancel
        //
        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (!isEdit)
            {
                
                dgv.Rows[rowIndex].Cells[0].Value = "";
            }
            isValid = true;
            this.Close();
        }

        private void txtPartNo_Leave(object sender, EventArgs e)
        {
            txtPartNo.Text = txtPartNo.Text.ToUpper();
        }

        private void txtPartName_Leave(object sender, EventArgs e)
        {
            txtPartName.Text = txtPartName.Text.ToUpper();
        }

        private void AddSpareParts_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.S)
                btnSave_Click(sender, e);
            if (e.KeyCode == Keys.Escape)
                btnCancel_Click(sender, e);
        }


    }
}